package cn.com.surker.production.service.impl;

import cn.com.surker.enums.StateEnum;
import cn.com.surker.enums.production.invoice.ShipTypeEnum;
import cn.com.surker.enums.sale.BillTimeTypeEnum;
import cn.com.surker.enums.system.IsAutoEnum;
import cn.com.surker.production.entity.InvoiceHandle;
import cn.com.surker.production.dao.InvoiceHandleDao;
import cn.com.surker.production.entity.vo.InvoiceHandleDetail;
import cn.com.surker.production.entity.vo.InvoiceScrapReport;
import cn.com.surker.production.service.InvoiceHandleService;
import cn.com.surker.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Value;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 生产退转料处理表 服务实现类
 * </p>
 *
 * @author admin
 * @since 2024-09-26
 */
public abstract class InvoiceHandleBaseImpl extends ServiceImpl<InvoiceHandleDao, InvoiceHandle> implements InvoiceHandleService {

	@Resource
	protected InvoiceHandleDao backOperateDao;

	@Value("${erp.datasource.base.db}")
	protected String baseDb;

	@Override
	public List<InvoiceHandle> findBySouInvId(Long orgId, Long invoiceId) {
		if (orgId == null || invoiceId == null) {
			return null;
		}
		QueryWrapper<InvoiceHandle> qw = new QueryWrapper<>();
		qw.eq("source_org_id", orgId);
		qw.eq("source_invoice_id", invoiceId);
		qw.eq("status", StateEnum.ENABLED.getCode());
		List<InvoiceHandle> operates = backOperateDao.selectList(qw);
		return operates;
	}


	public List<InvoiceHandle> findByTarInvId(Long orgId, Long invoiceId) {
		if (orgId == null || invoiceId == null) {
			return null;
		}
		QueryWrapper<InvoiceHandle> qw = new QueryWrapper<>();
		qw.eq("target_org_id", orgId);
		qw.eq("target_invoice_id", invoiceId);
		qw.eq("status", StateEnum.ENABLED.getCode());
		List<InvoiceHandle> operates = backOperateDao.selectList(qw);
		return operates;
	}

	@Override
	public List<InvoiceHandleDetail> findByPId(Long pid) {
		if (pid == null) {
			return null;
		}
		/*QueryWrapper<InvoiceHandle> qw = new QueryWrapper<>();
		qw.eq("status", StateEnum.ENABLED.getCode());
		qw.eq("pid", pid);*/
		List<InvoiceHandleDetail> backOperates = backOperateDao.findByPId(pid);
		return backOperates;
	}

	@Override
	public List<InvoiceHandleDetail> handleList(String erpDb, Long orgId, Long invId) {
		if (orgId == null || invId == null) {
			return null;
		}
		List<InvoiceHandleDetail> res = backOperateDao.handleList(erpDb, orgId, invId);
		return res;
	}


	@Override
	public IPage<InvoiceHandleDetail> findInvBackPage(String erpDb, String startDate, String endDate, String conName, String site, Long invoiceId, Long carId,
													  Integer isAuto, Integer handle, Integer pageIndex, Integer limit) {
		QueryWrapper<InvoiceHandle> qw = new QueryWrapper<>();
		qw.eq("a.status", StateEnum.ENABLED.getCode());
		if (StringUtils.isNotBlank(startDate)) {
			qw.ge("a.weight_time", startDate);
		}
		if (StringUtils.isNotBlank(endDate)) {
			qw.lt("a.weight_time", endDate);
		}
		if (StringUtils.isNotBlank(conName)) {
			qw.like("con.name", conName);
		}
		if (StringUtils.isNotBlank(site)) {
			qw.like("indent.site", site);
		}
		if (invoiceId != null) {
			//	发货单信息
			qw.eq("a.source_invoice_id", invoiceId);
		}
		if (carId != null) {
			qw.in("invoice.car_id", carId);
		}
		qw.eq("a.operate_type", ShipTypeEnum.BACK_SCRAP_WEIGHT.getCode());
		if (isAuto != null) {
			if (IsAutoEnum.SD.getCode().equals(isAuto)) {
				qw.eq("is_auto", IsAutoEnum.SD.getCode());
			} else {
				//	手动
				qw.and(e -> {
					e.isNull("is_auto").or().eq("is_auto", IsAutoEnum.ZD.getCode());
				});
			}
		}
		if (handle != null) {
			//	查询是否有处理记录,0没有,1有
			if (handle == 0) {
				qw.notExists("SELECT 1 FROM production_invoice_handle AS pih_sub WHERE pih_sub.pid = a.id");
			} else if (handle == 1) {
				qw.exists("SELECT 1 FROM production_invoice_handle AS pih_sub WHERE pih_sub.pid = a.id");
			}
		}
		qw.orderByDesc("a.id");
		qw.groupBy("a.id", "invoice.id");
		PageHelper.startPage(pageIndex, limit);
		Page<InvoiceHandleDetail> iPage = backOperateDao.findSouFullPage(new Page<>((pageIndex + limit) / limit, limit), qw, erpDb);
		/**分页插件**/
		if (!StringUtils.isNull(iPage.getRecords())) {
			PageInfo pageInfo = new PageInfo(iPage.getRecords());
			if (!StringUtils.isNull(pageInfo.getTotal()))
				iPage.setTotal(pageInfo.getTotal());
		}
		return iPage;
	}

	@Override
	public IPage<InvoiceScrapReport> findScrapPage(Long orgId, String dsCode, String startTime, String endTime, Integer pageIndex, Integer limit) {
		if (StringUtils.isBlank(dsCode)) {
			return null;
		}
		QueryWrapper<InvoiceScrapReport> qw = initScrapParams(orgId, startTime, endTime);
		PageHelper.startPage(pageIndex, limit);
		IPage<InvoiceScrapReport> scrapPage = backOperateDao.findScrap(new Page<>((pageIndex + limit) / limit, limit), dsCode, qw);
		return scrapPage;
	}


	public List<InvoiceScrapReport> findScrapList(Long orgId, String dsCode, String startTime, String endTime) {
		if (StringUtils.isAllParamNull(dsCode, startTime, endTime)) {
			return null;
		}
		QueryWrapper<InvoiceScrapReport> qw = initScrapParams(orgId, startTime, endTime);
		List<InvoiceScrapReport> scrapList = backOperateDao.findScrap(dsCode, qw);
		return scrapList;
	}

	@Override
	public IPage<InvoiceHandleDetail> findPage(String dbCode, Integer operateType, String startTime, String endTime, String searchVal, Integer pageIndex, Integer limit) {
		QueryWrapper<InvoiceHandle> qw = new QueryWrapper<>();
		qw.eq("a.status", StateEnum.ENABLED.getCode());
		//	操作类型
		if (operateType != null) {
			qw.eq("a.operate_type", operateType);
		}
		//	筛选时间
		if (StringUtils.isNotBlank(startTime)) {
			qw.ge("a.create_time", startTime);
		}
		if (StringUtils.isNotBlank(endTime)) {
			qw.lt("a.create_time", endTime);
		}
		//	搜索数据
		if (StringUtils.isNotBlank(searchVal)) {
			qw.and(e -> {
				e.like("souInv.code", searchVal).or().like("tarInv.code", searchVal).or().like("souInd.site", searchVal).or().like("tarInd.site", searchVal)
					.like("souCon.name", searchVal).or().like("tarCon.name", searchVal).or()
					.inSql("souInv.car_id", "SELECT tran_car_id from production_tran_con_car WHERE CODE like '%" + searchVal + "%' and org_id = souInv.org_id and status = 0" +
						" and (start_time < souInv.pro_time or start_time < souInv.create_time)").or()
					.inSql("tarInv.car_id", "SELECT tran_car_id from production_tran_con_car WHERE CODE like '%" + searchVal + "%' and org_id = tarInv.org_id and status = 0" +
						" and (start_time < tarInv.pro_time or start_time < tarInv.create_time)");
			});
		}
		qw.orderByDesc("a.create_time");
		PageHelper.startPage(pageIndex, limit);
		IPage<InvoiceHandleDetail> handlePage = backOperateDao.findPage(dbCode, new Page<>((pageIndex + limit) / limit, limit), qw);
		/**分页插件**/
		if (!StringUtils.isNull(handlePage.getRecords())) {
			PageInfo pageInfo = new PageInfo(handlePage.getRecords());
			if (!StringUtils.isNull(pageInfo.getTotal()))
				handlePage.setTotal(pageInfo.getTotal());
		}
		return handlePage;
	}

	/**
	 * 初始化作废参数
	 *
	 * @param startTime
	 * @param endTime
	 * @return
	 */
	private QueryWrapper<InvoiceScrapReport> initScrapParams(Long orgId, String startTime, String endTime) {
		QueryWrapper<InvoiceScrapReport> qw = new QueryWrapper<>();
		qw.eq("a.status", StateEnum.ENABLED.getCode());
		//	源站点
		qw.eq("a.source_org_id", orgId);
		//	发货单没有被删除
		qw.eq("invoice.status", StateEnum.ENABLED.getCode());
		//	只需要作废的操作
		qw.eq("a.operate_type", ShipTypeEnum.INVOICE_SCRAP.getCode());
		//	筛选时间
		if (StringUtils.isNotBlank(startTime)) {
			qw.ge("a.create_time", startTime);
		}
		if (StringUtils.isNotBlank(endTime)) {
			qw.lt("a.create_time", endTime);
		}
		return qw;
	}
}